<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="600" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="150px" label-position="left">
			<el-form-item label="选择租户" prop="tenant">
				<sc-table-select v-model="selectTenant" :apiObj="tenantIdApiObj" :table-width="600" :props="tenantIdProp">
					<template #header="{form, submit}">
						<el-form :inline="true" :model="form">
							<el-form-item>
								<el-input v-model="form.name" placeholder="租户名称" clearable></el-input>
							</el-form-item>
							<el-form-item>
								<el-button type="primary" @click="submit">查询</el-button>
							</el-form-item>
						</el-form>
					</template>
					<el-table-column prop="code" label="编号" width="100"></el-table-column>
					<el-table-column prop="name" label="租户名称"></el-table-column>
				</sc-table-select>
			</el-form-item>
			<el-form-item label="授权日期" prop="authDate">
				<el-date-picker v-model="form.authDate" type="date" value-format="YYYY-MM-DD" placeholder="选择授权日期"></el-date-picker>
			</el-form-item>
			<el-form-item label="本次授权到期日" prop="endDate">
				<el-date-picker v-model="form.endDate" type="date" value-format="YYYY-MM-DD" placeholder="选择本次授权到期日"></el-date-picker>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaving" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '新增',
					edit: '编辑',
					show: '查看'
				},
				visible: false,
				isSaving: false,
				//表单数据
				form: {
					id:"",
					tenantId: "",
					authDate:"",
					enDate:"",
				},
				selectTenant:{

				},
				tenantIdProp:{
					label: 'name',
					value: 'id',
					keyword: "name"
				},
				tenantIdApiObj: this.$API.system.tenant.list,
				//验证规则
				rules: {
					tenantId: [
						{required: true, message: '请选择租户'}
					],
					authDate:[
						{required: true, message: '请选择授权日期', trigger: 'change'}
					],
					endDate:[
						{required: true, message: '请选择本次授权到期日期', trigger: 'change'}
					],

				},
			}
		},
		mounted() {
		},
		watch: {
			selectTenant:{
				handler(){
					this.form.tenantId = this.selectTenant.id
				},
				deep: true
			}
		},
		methods: {
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this
			},
			//表单提交方法
			submit(){
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaving = true;
						var res;
						if(this.mode == 'edit')res = await this.$API.system.tenantAuth.edit.post(this.form)
						else if(this.mode == 'add')res = await this.$API.system.tenantAuth.save.post(this.form)
						this.isSaving = false;
						if(res.success){
							this.$emit('success', this.form, this.mode)
							this.visible = false;
							this.$message.success("操作成功")
						}
					}
				})
			},
			//表单注入数据
			setData(data){
				this.form.id = data.id
				this.form.tenantId = data.tenantId
				this.form.authDate = data.authDate
				this.form.endDate = data.endDate
				this.selectTenant = {
					id:data.tenantId,
					name:data.tenantName
				}
				//可以和上面一样单个注入，也可以像下面一样直接合并进去
				/*Object.assign(this.form, data)*/
			}
		}
	}
</script>

<style>
</style>
